<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\model\ding;

use app\api\model\ding\Seat as SeatModel;
use app\common\model\ding\SeatCategory as SeatCategoryModel;
use think\facade\Db;

/**
 * 商品评价模型
 * Class Article
 * @package app\api\model
 */
class SeatCategory extends SeatCategoryModel {
	/**
	 * 隐藏字段
	 * @var array
	 */
	protected $hidden = [
		'is_delete',
		'store_id',
		'create_time',
		'update_time',
	];

	public function add(int $shopId, array $contentList) {

		$this->deleteAll(['shop_id' => $shopId]);
		Db::name('ding_seat')->where('shop_id', $shopId)->delete();
		return $this->transaction(function () use ($shopId, $contentList) {
			foreach ($contentList as $content) {
				$data['name'] = $content['name'];
				$data['shop_id'] = $shopId;
				$data['create_time'] = time();
				$data['store_id'] = self::$storeId;
				$id = Db::name('ding_seat_category')->insertGetId($data);
				// 内容

				SeatModel::increased($shopId, (int) $id, $content['seat']);

			}
			return true;
		});
	}
	/**
	 * 获取列表--预订本
	 * @param int $categoryId
	 * @param int $limit
	 * @return \think\Paginator
	 * @throws \think\db\exception\DbException
	 */
	public function getListYuding(int $shopId = 0, $params) {
		if ($params['date'] == '') {
			$params['date'] = date('Y-m-d');
		}
		// 检索查询条件
		$filter = [];
		// 获取列表数据
		$lists = $this

			->where('is_delete', '=', 0)
			->order(['create_time' => 'desc'])
			->select()->toArray();

		foreach ($lists as $key => $list) {
			$seats = Db::name('ding_seat')->where('shop_id', $shopId)
				->where('category_id', $list['category_id'])->select()->toArray();
			foreach ($seats as $k => $value) {
				$order = Db::name('ding_order_seat')->alias('o')
					->join('store_shop_clerk u', 'u.clerk_id=o.clerk_id','left')
					->join('ding_letter l', 'l.type_id=o.letter_type',)
					->join('ding_order order', 'order.id=o.order_id')
					->where('o.seat_id', $value['seat_id'])
					->where('o.status', 'in', [10, 20])
					->where('o.date', $params['date'])
					->where('order.type', $params['type'])
					->where('o.is_delete', 0)
					->field('o.*,u.real_name as clerk_name,l.name as letter_type,order.real_name,order.sex,order.mobile,order.to_people,
						order.source,order.member_id,order.clerk_id,order.week,order.sex,order.remark,order.unit,order.ding_money')
					->find();

				$seats[$k]['status'] = $order ? $order['status'] : $value['status'];
				$is_past = 0;
				if ($seats[$k]['status'] == 10 || $seats[$k]['status'] == 20) {
					if ($order) {
						$seats[$k]['order_id'] = $order['order_id'];
						$seats[$k]['mobile'] = $order['mobile'];
						$seats[$k]['unit'] = $order['unit'];
						$seats[$k]['ding_money'] = $order['ding_money'];
						$seats[$k]['to_people'] = $order['to_people'];
						$seats[$k]['real_name'] = $order['real_name'];
						$seats[$k]['clerk_name'] = $order['clerk_name'];
						$seats[$k]['letter_type'] = $order['letter_type'];
						$seats[$k]['source'] = $order['source'];
						$seats[$k]['time'] = $order['time'];
						$seats[$k]['sex'] = $order['sex'];
						$seats[$k]['week'] = $order['week'];
						$seats[$k]['date'] = $order['date'];
						$seats[$k]['clerk_id'] = $order['clerk_id'];
						$seats[$k]['type'] = $order['type'];
						$seats[$k]['member_id'] = $order['member_id'];
						$seats[$k]['is_lian'] = $order['is_lian'];
						$seats[$k]['remark'] = $order['remark'];
						$time = strtotime($order['date'] . ' ' . $order['time']);
						if ($time <= time()) {
							$is_past = 1;
						}
					}
				}
				$expired = 0;
				if ($order && strtotime($order['date']) < strtotime(date('Y-m-d'))) {

					$expired = 1;
				}

				$seats[$k]['expired'] = $expired; //过期不可操作
				$seats[$k]['is_past'] = $is_past;
			}
			$lists[$key]['seat'] = $seats;
		}
		return $lists;
	}

	/**
	 * 获取列表--转台
	 * @param int $categoryId
	 * @param int $limit
	 * @return \think\Paginator
	 * @throws \think\db\exception\DbException
	 */
	public function getListChange(int $shopId = 0, $params) {

		// 检索查询条件
		$filter = [];
		// 获取列表数据
		$lists = $this
			->order(['create_time' => 'desc'])
			->select()->toArray();

		foreach ($lists as $key => $list) {
			$seats = Db::name('ding_seat')
				->where('shop_id', $shopId)->where('category_id', $list['category_id'])
				->where('status', 0)
				->select()->toArray();
			foreach ($seats as $k => $value) {
				$order = Db::name('ding_order_seat')->alias('o')

					->join('ding_order order', 'order.id=o.order_id')
					->where('o.seat_id', $value['seat_id'])
					->where('o.status', 'in', [10])
					->where('o.date', $params['date'])
					->where('o.type', $params['type'])
					->where('o.is_delete', 0)
					->field('o.*')
					->find();

				$is_can = 1; //修改
				if ($order && $order['order_id'] != $params['orderId']) {
					$is_can = 2; //调换

				}
				if ($order && $order['order_id'] == $params['orderId']) {
					$is_can = 0; //不可修改
				}
				if (strtotime($params['date']) < strtotime(Date('Y-m-d'))) {
					$is_can = 0;
				}

				$seats[$k]['is_can'] = $is_can;
			}
			$lists[$key]['seat'] = $seats;
		}
		return $lists;
	}

	/**
	 * 获取列表--安排落座
	 * @param int $categoryId
	 * @param int $limit
	 * @return \think\Paginator
	 * @throws \think\db\exception\DbException
	 */
	public function getListYuyue(int $shopId = 0, $params) {
		if ($params['date'] == '') {
			$params['date'] = date('Y-m-d');
		}
		// 检索查询条件
		$filter = [];
		// 获取列表数据
		$lists = $this
			->where('is_delete', '=', 0)
			->order(['create_time' => 'desc'])
			->select()->toArray();

		foreach ($lists as $key => $list) {
			$seats = Db::name('ding_seat')->where('category_id', $list['category_id'])->where('shop_id', $shopId)->select()->toArray();
			$newArr = [];
			foreach ($seats as $k => $value) {
				$order = Db::name('ding_order_seat')->alias('o')
					->join('store_shop_clerk u', 'u.clerk_id=o.clerk_id')
					->join('ding_letter l', 'l.type_id=o.letter_type')
					->where('o.seat_id', $value['seat_id'])
					->where('o.status', 'in', [10, 20])
					->where('o.date', $params['date'])
					->where('o.type', $params['type'])
					->where('o.is_delete', 0)
					->field('o.*,u.real_name as clerk_name,l.name as letter_type')
					->find();

				$seats[$k]['status'] = $order ? $order['status'] : 0;
				$is_past = 0;
				if ($seats[$k]['status'] == 10 || $seats[$k]['status'] == 20) {

				} else {
					$newArr[] = $value;
				}

			}
			$lists[$key]['seat'] = $newArr;
		}
		return $lists;
	}

	/**
	 * 获取列表
	 * @param int $categoryId
	 * @param int $limit
	 * @return \think\Paginator
	 * @throws \think\db\exception\DbException
	 */
	public function getListShop(int $shopId = 0, $params) {
		// 检索查询条件
		$filter = [];
		// 获取列表数据
		$lists = $this->with(['seat'])

			->where('shop_id', '=', $shopId)
			->where('is_delete', '=', 0)
			->order(['create_time' => 'desc'])
			->select()->toArray();

		return $lists;
	}

}